library(magrittr)
library(stringr)
library(plyr)
library(tidyverse)
library(readxl)
library(gridExtra)


rm(list=ls())
home = 'C:/Users/jdt34/Dropbox/VNA_Responsiveness/Short Article/JOP-dataverse/'


delegates = paste0(home, 'Data/individual-outcomes.xlsx') %>%
  read_xlsx %>%
  mutate(ID=Original) %>%
  subset(select=c(ID,EduCareer,EduYears,EduLevel))


pooled_texts = paste0(home, 'Data/pooled-outcomes.xlsx') %>%
  read_xlsx %>%
  transform(Treatment=factor(Treatment, levels=c('Control','Citizen','Firm')),
            Dosage=Prop.Citizen+Prop.Firm)


cit.ind = subset(pooled_texts, 
                 !is.na(Treatment)) %>%
  ggplot(aes(x=Prop.Citizen)) + 
  geom_histogram(color='black', binwidth=0.01) +
  geom_text(x=0.9, y=90, label='Delegates in provinces', hjust=1) +
  labs(x='% receiving citizen treatment', y='Count') +
  scale_x_continuous(breaks=seq(from=0,
                                to=1,
                                by=0.2)-0.005,
                     labels=c('0%','20%','40%','60%','80%','100%')) +
  coord_cartesian(xlim=c(0, 1)-0.005, ylim=c(0, 120), expand=F) +
  theme_minimal() +
  theme(panel.border=element_rect(fill=NA, color='black'),
        plot.margin=unit(c(0.1, 0.2, 0, 0), units='in'))


firm.ind = subset(pooled_texts, 
                  !is.na(Treatment)) %>%
  ggplot(aes(x=Prop.Firm)) + 
  geom_histogram(color='black', binwidth=0.01) +
  geom_text(x=0.9, y=90, label='Delegates in provinces', hjust=1) +
  labs(x='% receiving firm treatment', y='Count') +
  scale_x_continuous(breaks=seq(from=0,
                                to=1,
                                by=0.2)-0.005,
                     labels=c('0%','20%','40%','60%','80%','100%')) +
  coord_cartesian(xlim=c(0, 1)-0.005, ylim=c(0, 120), expand=F) +
  theme_minimal() +
  theme(panel.border=element_rect(fill=NA, color='black'),
        plot.margin=unit(c(0.1, 0.2, 0, 0), units='in'))


cit.prov = subset(pooled_texts, 
                  !is.na(Treatment)) %>%
  distinct(Province, .keep_all=T) %>%
  ggplot(aes(x=Prop.Citizen)) + 
  geom_histogram(color='black', binwidth=0.01) +
  geom_text(x=0.9, y=16, label='Provinces', hjust=1) +
  labs(x='% receiving citizen treatment', y='Count') +
  scale_x_continuous(breaks=seq(from=0,
                                to=1,
                                by=0.2)-0.005,
                     labels=c('0%','20%','40%','60%','80%','100%')) +
  scale_y_continuous(breaks=seq(from=0,
                                to=20,
                                by=4)) +
  coord_cartesian(xlim=c(0, 1)-0.005, ylim=c(0, 20), expand=F) +
  theme_minimal() +
  theme(panel.border=element_rect(fill=NA, color='black'),
        plot.margin=unit(c(0.1, 0.2, 0, 0), units='in'))


firm.prov = subset(pooled_texts, 
                   !is.na(Treatment)) %>%
  distinct(Province, .keep_all=T) %>%
  ggplot(aes(x=Prop.Firm)) + 
  geom_histogram(color='black', binwidth=0.01) +
  geom_text(x=0.9, y=16, label='Provinces', hjust=1) +
  labs(x='% receiving firm treatment', y='Count') +
  scale_x_continuous(breaks=seq(from=0,
                                to=1,
                                by=0.2)-0.005,
                     labels=c('0%','20%','40%','60%','80%','100%')) +
  scale_y_continuous(breaks=seq(from=0,
                                to=20,
                                by=4)) +
  coord_cartesian(xlim=c(0, 1)-0.005, ylim=c(0, 20), expand=F) +
  theme_minimal() +
  theme(panel.border=element_rect(fill=NA, color='black'),
        plot.margin=unit(c(0.1, 0.2, 0, 0), units='in'))


g = grid.arrange(cit.ind, firm.ind, cit.prov, firm.prov, 
                 nrow=2)


ggsave(filename='figure-A05-1.png', 
       plot=g, 
       path=paste0(home, 'Figures/'), 
       width=7, 
       height=4.75,
       units='in')
ggsave(filename='figure-A05-1.eps', 
       plot=g, 
       path=paste0(home, 'Figures/'), 
       width=7, 
       height=4.75,
       units='in', 
       device=cairo_ps)


